package chat4_tedu.mychatClient.ui;

import chat4_tedu.mychatClient.Client;
import chat4_tedu.mychatClient.utils.IOUtil;
import chat4_tedu.mychatClient.vo.Request_Command;
import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
import java.io.OutputStream;

public class RegWindow {

	private JFrame jFrame = null;
	private JPanel jContentPane = null;
	private JLabel jLabelRegUserName = null;
	private JLabel jLabelRegPassword = null;
	private JLabel jLabelRegRepassword = null;
	private JTextField jTextFieldRegUserName = null;
	private JPasswordField jTextFieldRegPassword = null;
	private JPasswordField jTextFieldRegRepassword = null;
	public JButton jButtonReg = null;
	public JButton jButtonLogin = null;
	private InputStream in;  
	private OutputStream out;  //  @jve:decl-index=0:
//	private GetServerMsgThread gif;
	private Client client;

	public RegWindow(Client client){
		this.client = client;
		this.client.setReg(this);
		getJFrame();
	}


	public void closeFrame(){
		this.jFrame.dispose();
		this.client.setReg(null);
	}



	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setSize(new Dimension(398, 337));
			jFrame.setTitle("注册");
			jFrame.setResizable(false);



			if (jContentPane == null) {
				jContentPane = new JPanel();
				jContentPane.setLayout(null);

				jLabelRegRepassword = new JLabel();
				jLabelRegRepassword.setBounds(new Rectangle(15, 162, 357, 47));
				jLabelRegRepassword.setFont(new Font("Dialog", Font.BOLD, 18));
				jLabelRegRepassword.setText("  确认密码：");
				jLabelRegPassword = new JLabel();
				jLabelRegPassword.setBounds(new Rectangle(15, 94, 357, 47));
				jLabelRegPassword.setFont(new Font("Dialog", Font.BOLD, 18));
				jLabelRegPassword.setText("  密       码：");
				jLabelRegUserName = new JLabel();
				jLabelRegUserName.setBounds(new Rectangle(15, 31, 357, 47));
				jLabelRegUserName.setFont(new Font("Dialog", Font.BOLD, 18));
				jLabelRegUserName.setText("  昵       称：");

				jContentPane.add(jLabelRegUserName, null);
				jContentPane.add(jLabelRegPassword, null);
				jContentPane.add(jLabelRegRepassword, null);


				if (jTextFieldRegUserName == null) {
					jTextFieldRegUserName = new JTextField();
					jTextFieldRegUserName.setBounds(new Rectangle(110, 34, 258, 41));
				}
				jContentPane.add(jTextFieldRegUserName, null);
//				jContentPane.add(getJTextFieldRegUserName(), null);


				if (jTextFieldRegPassword == null) {
					jTextFieldRegPassword = new JPasswordField();
					jTextFieldRegPassword.setBounds(new Rectangle(110, 97, 258, 41));
				}
				jContentPane.add(jTextFieldRegPassword, null);
//				jContentPane.add(getJTextFieldRegPassword(), null);


				if (jTextFieldRegRepassword == null) {
					jTextFieldRegRepassword = new JPasswordField();
					jTextFieldRegRepassword.setBounds(new Rectangle(111, 165, 258, 41));
				}
				jContentPane.add(jTextFieldRegRepassword, null);
//				jContentPane.add(getJTextFieldRegRepassword(), null);


				if (jButtonReg == null) {
					jButtonReg = new JButton();
					jButtonReg.setBounds(new Rectangle(146, 221, 131, 49));
					jButtonReg.setText("注册");
					jButtonReg.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							doReg();
						}
					});
				}
				jContentPane.add(jButtonReg, null);
//				jContentPane.add(getJButtonReg(), null);


				if (jButtonLogin == null) {
					jButtonLogin = new JButton();
					jButtonLogin.setBounds(new Rectangle(289, 266, 88, 28));
					jButtonLogin.setText("去登陆");
					jButtonLogin.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							//如果点击了去登陆按钮
							//关闭注册框
							closeFrame();
							//打开登陆窗口
							LoginWindow loginWindow=new LoginWindow(client);
							client.setLoginWin(loginWindow);
						}
					});
				}
				jContentPane.add(jButtonLogin, null);
//				jContentPane.add(getJButtonLogin(), null);
			}
			jFrame.setContentPane(jContentPane);
//			jFrame.setContentPane(getJContentPane());
			jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent e) {
					System.exit(0);
				}
			});
			jFrame.setLocation(410,210);
			jFrame.setVisible(true);
		}
		return jFrame;
	}





//	注册方法
	private void doReg(){
		try{
			//连接服务器端
//			this.linkInfo.initSocket();////////////////////////////////////////////////////////////////////////////////////
			if(this.client.getSocket()!=null){
				//做一些简单的验证，实际上这里应该有比较复杂的验证，比如敏感字符
				if(jTextFieldRegUserName.getText()!=null&&!jTextFieldRegUserName.getText().equals("")&&
						jTextFieldRegPassword.getText()!=null&&!jTextFieldRegPassword.getText().equals("")&&
						jTextFieldRegRepassword.getText()!=null&&!jTextFieldRegRepassword.getText().equals("")){
					if(!jTextFieldRegRepassword.getText().equals(jTextFieldRegPassword.getText())){
//						this.linkInfo.getAlert().showAlert("两次密码输入不一致");
						JOptionPane.showMessageDialog(null, "两次密码输入不一致");
					}else{
						if(in==null){
							in = this.client.getSocket().getInputStream();
						}
						if(out==null){
							out = this.client.getSocket().getOutputStream();
						}
						//如果获取来自服务器端信息的线程没有启动，则启动该线程
						/*if(gif==null){
							gif = new GetServerMsgThread(this.linkInfo);
							new Thread(gif).start();
						}*/
						//注册过程中，使注册界面上的注册按钮不可用
						jButtonReg.setEnabled(false);
//						注册过程中，使注册界面上的去登陆按钮不可用
						jButtonLogin.setEnabled(false);
						this.client.setMe(jTextFieldRegUserName.getText());
						IOUtil.writeShort(Request_Command.REG, out);
						IOUtil.writeString(jTextFieldRegUserName.getText(), out);
						IOUtil.writeString(jTextFieldRegPassword.getText(), out);
					}
				}else{
					JOptionPane.showMessageDialog(null, "用户名或密码为空");
//					this.linkInfo.getAlert().showAlert("用户名或密码为空");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
